package cn.wildfire.chat.app.extendsion.classes;

import android.content.Context;
import android.widget.Toast;

import org.json.JSONObject;

import java.util.HashMap;

import cn.wildfire.chat.app.extendsion.https.OkHttpRequest;
import cn.wildfire.chat.app.extendsion.https.ReqCallBack;
import cn.wildfire.chat.app.extendsion.https.ServerUrlClient;
import cn.wildfire.chat.app.extendsion.utils.TextUtils;

/**
 * Create by HEKE, 2019/8/16 17:39
 * TODO 发送验证码操作
 */
public class SendCodeRequest {

    Context mContext;
    String phoneNumber;
    String regionId;

    private OnSendCodeListener onSendCodeListener;

    protected ViewTimerSet viewTimerSet;

    public SendCodeRequest(Context context, String phoneNumber, String regionId) {
        this.mContext = context;
        this.phoneNumber = phoneNumber;
        this.regionId = regionId;
        sendCode();
    }

    public SendCodeRequest(Context context, String phoneNumber, String regionId, ViewTimerSet viewTimerSet) {
        this.mContext = context;
        this.phoneNumber = phoneNumber;
        this.regionId = regionId;
        this.viewTimerSet = viewTimerSet;
        sendCode();
    }

    public SendCodeRequest(Context context, String phoneNumber, String regionId, OnSendCodeListener onSendCodeListener) {
        this.mContext = context;
        this.phoneNumber = phoneNumber;
        this.regionId = regionId;
        this.onSendCodeListener = onSendCodeListener;
        sendCode();
    }

    private void sendCode() {
        if (TextUtils.isTextEmpty(phoneNumber)) {
            Toast.makeText(mContext, "请输入手机号!", Toast.LENGTH_SHORT).show();
            return;
        }
        Toast.makeText(mContext, "请求验证码...", Toast.LENGTH_SHORT).show();
        String url = ServerUrlClient.SEND_CODE;
        HashMap<String, String> params = new HashMap<>();
        params.put("mobile", phoneNumber);
        params.put("regionId", regionId);
        OkHttpRequest.getInstance(mContext).requestAsyn(url, OkHttpRequest.TYPE_POST_JSON, params, new ReqCallBack<String>() {
            @Override
            public void onReqSuccess(String result) {
                try {
                    if (!TextUtils.isTextEmpty(result)) {
                        JSONObject jsonObject = new JSONObject(result);
                        String code = jsonObject.getString("code").trim();
                        String message = jsonObject.getString("message");
                        if (code.equals("0")) {
                            Toast.makeText(mContext, "已发送验证码!", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(mContext, "发送失败!" + message, Toast.LENGTH_SHORT).show();
                        }
                        if (onSendCodeListener != null) {
                            onSendCodeListener.send(code.equals("0"));
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onReqFailed(String errorMsg) {
                if (onSendCodeListener != null) {
                    onSendCodeListener.send(false);
                }
            }
        }).request();
    }

    /**
     * Create by HEKE, 2019/10/17 9:53
     * TODO 发送验证码回调接口
     */
    public interface OnSendCodeListener {
        void send(boolean success);
    }

}
